package com.ruoyi.erp.controller;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.domain.ProjectMember;
import com.ruoyi.erp.domain.StaffingSetting;
import com.ruoyi.erp.service.IProjectMemberService;
import com.ruoyi.erp.service.IStaffingSettingService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.Project;
import com.ruoyi.erp.service.IProjectService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目Controller
 * 
 * @author sauger
 * @date 2022-03-09
 */
@Controller
@RequestMapping("/erp/project")
public class ProjectController extends BaseController
{
    private String prefix = "erp/project";

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IProjectMemberService projectMemberService;

    @Autowired
    private ISysPostService sysPostService;

    @Autowired
    private IStaffingSettingService staffingSettingService;

    @RequiresPermissions("erp:project:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    /**
     * 查询项目列表
     */
    @RequiresPermissions("erp:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @RequiresPermissions("erp:project:export")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        return util.exportExcel(list, "项目数据");
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add()
    {
         return prefix + "/add";
    }

    /**
     * 新增保存项目
     */
    @RequiresPermissions("erp:project:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Project project)
    {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目
     */
    @RequiresPermissions("erp:project:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Project project = projectService.selectProjectById(id);
        mmap.put("project", project);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目
     */
    @RequiresPermissions("erp:project:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Project project)
    {
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("erp:project:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(projectService.deleteProjectByIds(ids));
    }


    /**
     * 项目成员相关controller
     */

    @RequiresPermissions("erp:project:member:view")
    @GetMapping("/{id}/member")
    public String projectMemberView(@PathVariable("id") Integer projectId, ModelMap mmap)
    {
        Project project = projectService.selectProjectById(projectId);
        if (StringUtils.isNull(project)) throw new ServiceException("参数错误");
        mmap.put("project", project);
        return prefix + "/member/member";
    }

    @RequiresPermissions("erp:project:member:view")
    @PostMapping("/{id}/member/list")
    @ResponseBody
    public TableDataInfo getProjectMemberList(@PathVariable("id") Integer projectId, ProjectMember projectMember)
    {
        startPage();
        if (StringUtils.isNull(projectMember) || StringUtils.isNull(projectMember.getOnboardStatus())){
            //默认只显示在岗的
            projectMember.setOnboardStatus("onboard");
        }else if (projectMember.getOnboardStatus().equals("all")){
            projectMember.setOnboardStatus(null);
        }
        projectMember.setProjectId(projectId);
        return getDataTable(projectMemberService.selectProjectMemberList(projectMember));
    }

    @RequiresPermissions("erp:project:member:add")
    @GetMapping("/{id}/member/add")
    public String addMember(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Project project = projectService.selectProjectById(id);
        if (StringUtils.isNull(project)) throw new ServiceException("参数错误");
        List<SysPost> posts = sysPostService.selectPostList(null);

        StaffingSetting settings = staffingSettingService.selectStaffingSettingByResourceWithDefault("project", id);
        mmap.put("project", project);
        mmap.put("posts",posts);
        mmap.put("staffingSetting", settings);
        return prefix + "/member/add";
    }

    @RequiresPermissions("erp:project:member:add")
    @PostMapping("/{id}/member/add")
    @ResponseBody
    public AjaxResult saveAddMember(@PathVariable("id") Integer id, ProjectMember projectMember)
    {
        return toAjax(projectMemberService.insertProjectMember(projectMember));
    }

    @RequiresPermissions("erp:project:member:edit")
    @GetMapping("/{projectId}/member/edit/{id}")
    public String editProjectMember(@PathVariable("projectId") Integer projectId, @PathVariable("id") Integer id, ModelMap mmap)
    {
        ProjectMember projectMember = projectMemberService.selectProjectMemberById(id);
        mmap.put("projectMember", projectMember);
        mmap.put("isDepart", Boolean.valueOf(false));
        List<SysPost> posts = sysPostService.selectPostList(null);
        mmap.put("posts",posts);
        return prefix + "/member/edit";
    }

    @RequiresPermissions("erp:project:member:remove")
    @GetMapping("/{projectId}/member/depart/{id}")
    public String departProjectMember(@PathVariable("projectId") Integer projectId, @PathVariable("id") Integer id, ModelMap mmap)
    {
        ProjectMember projectMember = projectMemberService.selectProjectMemberById(id);
        mmap.put("projectMember", projectMember);
        mmap.put("isDepart", Boolean.valueOf(true));
        List<SysPost> posts = sysPostService.selectPostList(null);
        mmap.put("posts",posts);
        return prefix + "/member/edit";
    }

    @RequiresPermissions(value = {"erp:project:member:remove", "erp:project:member:edit"}, logical = Logical.OR)
    @PostMapping("/{projectId}/member/edit")
    @ResponseBody
    public AjaxResult saveEditProjectMember(ProjectMember projectMember)
    {
        return toAjax(projectMemberService.updateProjectMember(projectMember));
    }

    @PostMapping("/info")
    @ResponseBody
    public Project getInfo(Integer id)
    {
        return projectService.selectProjectById(id);
    }

}
